package cn.qu.phizline.connect.handle;

import cn.qu.phizline.PhizlineApplication;
import cn.qu.phizline.connect.WebSocketChannelEvents;
import cn.qu.phizline.event.common.ResponseEvent;
import cn.qu.phizline.event.init.InitEvent;

import static cn.qu.phizline.activity.IntentExtraKeys.PUBLISH_KEY;

/**
 * Created by zh on 2017/10/26.
 */

public class InitHandle implements Handle {

    @Override
    public void handle(WebSocketChannelEvents events, ResponseEvent payload) {
        ResponseEvent.ResponseBody body = (ResponseEvent.ResponseBody) payload.getBody();
        if (body.getCode() == InitEvent.CODE_SUCCESS) {
            events.onInit(true, PhizlineApplication.getToken());
        } else if (body.getCode() == InitEvent.CODE_FAILURE) {
            String publishKey = (String) body.getData();
            PhizlineApplication.putAttr(PUBLISH_KEY, publishKey);
            events.onInit(false,publishKey);
        }
    }

}
